<?php
require_once(dirname(__FILE__).'/config.php');

$menutype = 'mydede';
$menutype_son = 'op';
require_once(DEDEINC.'/datalistcp.class.php');
setcookie('ENV_GOBACK_URL', GetCurUrl(), time()+3600, '/');
if(!isset($dopost))
{
	$dopost = '';
}

if(in_array($dopost,array('','del','buy'))){
	CheckRank(0,0);
}

if($dopost=='')
{
	$query = "Select * From `#@__member_operation` where mid='".$cfg_ml->M_ID."' And product='archive' order by aid desc";
	$dlist = new DataListCP();
	$dlist->pageSize = 20;
	$dlist->SetTemplate(DEDEMEMBER.'/templets/mypay.htm');
	$dlist->SetSource($query);
	$dlist->Display();
}
elseif($dopost=='del')
{
	$ids = ereg_replace("[^0-9,]","",$ids);
	$query = "Delete From `#@__member_operation` where aid in($ids) And mid='{$cfg_ml->M_ID}' And product='archive'";
	$dsql->ExecuteNoneQuery($query);
	ShowMsg("删除成功","mypay.php");
	exit();
}elseif($dopost=='buy'){
	$aid = intval($aid);
	$artinfo = $dsql->getOne("Select * from `#@__addonarticle` a inner join `#@__archives` c on a.aid = c.id and a.aid='$aid'");
	
	if($artinfo['integral'] > $cfg_ml->M_Scores){
		ShowMsg("积分不够！",$refer);
		exit;
	}
	
	$check = "Select * From `#@__member_operation` where mid='".$cfg_ml->M_ID."' And product='archive' And pid='$aid'";
	$check = $dsql->getOne($check);
	if(!empty($check) && isset($check['buyid'])){
		ShowMsg("已经购买过了！",$refer);
		exit;
	}
	
	
	if($artinfo['isfree'] == '收费'){
		$query1 = "Insert into `#@__member_operation` (buyid,pname,product,money,mtime,pid,mid,oldinfo) VALUES ('ARCHIVE{$aid}','文章购买','archive','{$artinfo['integral']}','".time()."','{$aid}','{$cfg_ml->M_ID}','{$artinfo['title']}')";
		$query2 = "Update `#@__member` set scores = scores-{$artinfo['integral']} where mid = '{$cfg_ml->M_ID}'";
		$dsql->ExecuteNoneQuery($query1);
		$dsql->ExecuteNoneQuery($query2);
		ShowMsg("购买成功！","mypay.php");
	}
}elseif($dopost=='check'){
	$mid = intval($cfg_ml->M_ID);
	$content = '';
	if($mid > 0){
		$check = "Select * From `#@__member_operation` where mid='".$cfg_ml->M_ID."' And product='archive' And pid='$aid'";
		$check = $dsql->getOne($check);
		if(!empty($check) && isset($check['buyid'])){
			$query = "Select body from `#@__addonarticle` where aid='$aid'";
			$body = $dsql->getOne($query);
			$content = $body['body'];
		}
	}
	echo $content;
}
?>